<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;

class Weixin extends Base
{
    // 所属工号列表接口
    public function manageList()
    {
        $data = input();//token

        // $manageInfo = $this->manageGroupInfo($data['manage_id']);
        // if ($manageInfo['group_name'] == '普通销售') {
        //     $where['id'] = $data['manage_id'];
        // }elseif ($manageInfo['group_name'] == '销售主管') {
        //     $where['zu_id'] = $manageInfo['zu_id'];
        // }elseif ($manageInfo['group_name'] == '销售经理') {
        //     $where['project_id'] = $manageInfo['project_id'];
        // }else{

        // }
        $where['status'] = 1;
        // $where['is_xiaoshou'] = 1;

        $manageList = db('crm_manage')->field('id,username,name')->where($where)->order('is_xiaoshou DESC,zu_id asc')->select();
        apiReturn(200,'获取所属工号列表成功',$manageList);
    }

    // 新增修改微信接口
    public function weixinAdd()
    {
        $data = input();//token id weixin manage_id fans weixin_id is_tuiguang

        if (empty($data['weixin'])) {
            apiReturn(201,'微信号不能为空');   
        }
        if (empty($data['manage_id'])) {
            apiReturn(202,'所属工号不能为空');   
        }
        if (empty($data['weixin_id'])) {
            apiReturn(203,'微信ID不能为空');
        }

        $data['status'] = 1;
        $data['date'] = time();

        if (empty($data['id'])) {
            db('crm_weixin')->insert($data);
            apiReturn(200,'新增微信号成功');
        }else{
            db('crm_weixin')->where('id',$data['id'])->update($data);
            apiReturn(200,'修改微信号成功');
        }
    }

    // 微信列表接口
    public function weixinList()
    {
        $data = input();//token manage_id weixin weixin_id rows page

        $where = [];
        $manage_author = $this->getManageAuthor();
        if ($manage_author['is_xiaoshou'] == 1) {
            $where['manage_id'] = $this->getManageList();
        }
        if (isset($data['manage_id'])) {
            if (!empty($data['manage_id'])) {
                $where['manage_id'] = $data['manage_id'];
            }
        }
        if (isset($data['weixin'])) {
            if (!empty($data['weixin'])) {
                $where['weixin'] = array('like','%'.$data['weixin'].'%');
            }
        }
        if (isset($data['weixin_id'])) {
            if (!empty($data['weixin_id'])) {
                $where['weixin_id'] = array('like','%'.$data['weixin_id'].'%');
            }
        }

        $weixinList = db('crm_weixin')->where($where)->order('date DESC')->limit(($data['page']-1)*$data['rows'],$data['rows'])->select();
        foreach ($weixinList as $key => $value) {
            $manage_info = db('crm_manage')->where('username',$value['manage_id'])->find();
            $weixinList[$key]['manage_name'] = $manage_info['username'].'：'.$manage_info['name'];
        }

        $count = db('crm_weixin')->where($where)->count();

        $datas = array(
            'weixinList'=>$weixinList,
            'count'=>$count
        );
        apiReturn(200,'获取微信列表成功',$datas);
    }

    // 获取微信信息
    public function weixinInfo()
    {
        $data = input();//token id
        $weixinInfo = db('crm_weixin')->where('id',$data['id'])->find();
        apiReturn(200,'获取微信信息成功',$weixinInfo);
    }

    // 删除微信号
    public function weixinDelete()
    {
        $data = input();//token id
        if (empty($data['id'])) {
            apiReturn(201,'微信号id为空');
        }
        db('crm_weixin')->where('id',$data['id'])->delete();
        apiReturn(200,'删除微信成功');
    }

}
